Variables

A variable is a container which holds the data or the value the user gives. variables are memory locations in a computer's memory to store data. variable is varying it can take different values during times during execution. 

Every variable in C has three most fundamental attributes. They are:

  • Name

  • Value

  • Address


Guidelines for naming a variable:

  • A variable must begin with either a letter or an underscore(_).

  • C is a case sensitive, hence upper and lower case letters are treated as different. For example sum is different SUM

  • Only “ _ “ is used as a special symbol, other symbols are not permitted.

  • variable should not be a keyword.

Valid Variables:

Sum, count, area, roll_no

Invalid Variables:

  • roll no - No blank spaces are allowed

  • %marks - Special symbols are not allowed

  • int - Keyword cannot be a variable.

Declaration of variable:

A variable declaration tells the compiler where and how much storage is needed to create the variable. a variable definition specifies a data type and contains a list of one or more variables of that type.

Syntax: data_type variable1, variable2 

variable1 - name of the variable

variables are separated by comma. The statement should always end with a semicolon.

Example: int radius, count

Assignment/Initialization of variables

Values can be assigned with a value using assignment operator (=)

Syntax: varible_name = constant

Example: sum =100;

The value of a variable can be assigned during the declaration of the variable. This process is called initialization. C variables declared can be initialized with the help of assignment operator ‘=’.

syntax: data_type variable_name = constant;

Example: int roll_no = 596403


Sample program of declarations, assignment and values stored in variable.

#include <stdio>

int main() {

//Declaration

int sum1, sum_2, count;

float avg;

//Declaration and initialization

char letter = ‘A’;

//Assign values

sum1 = 200;

sum_2=30;

count = sum1+sum_2;

avg = count/2;

//Printing values

printf(“The sum of two number is: %d”, count);

printf(“The average of two number is: %f”, avg);

printf(“Character is: %c”, letter);

return 0;

}

Getting input through the user:

The data can be read from the user through the keyboard using scanf function.

syntax: scanf(“control string” ,&variable1, &variable2);

The control string represents the type of the variables specified. “&” symbol specifies the address of the variable.

Example: 

scanf(“%d%f” , &sum, &avg);

where,

%d refers to integer value

%f refers to float value

Sample program using scanf function:

#include <stdio>

int main(){

int length, breadth, area;

printf(“Enter the length and breadth of a rectangle”);

scanf(“%d%d”, &length, &breadth);

area = length*breadth;

printf(“Area of a rectangle is”, area);

return 0;

}

Constant variables:

Variables having an unchanged value during the execution of the program are referred to as constant Variables. A constant variable can be declared by using the keyword “const”.

Example: const int value = 100;

Volatile Variable:

variables which can be changed at any time by some external sources from outside or same program are called volatile variables.

Syntax: volatile datatype variable;


← Back Next →

Comments

Popular posts from this blog

Wrapper Class

Information Security & Essential Terminology

Information Security Threat Categories